﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace GUI
{
    public partial class usTimKiem : UserControl
    {
        public usTimKiem()
        {
            InitializeComponent();
        }

        private void pnNangCao_Paint(object sender, PaintEventArgs e)
        {

        }

        private void rdo_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Name=="rdoCoBan")
            {
                pnNangCao.Visible = false;
            }
            else
            {
                pnNangCao.Visible = true;
            }
            
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            gvTimkiem.Rows.Clear();
            if(rdoCoBan.Checked==true)
                Search_CB();
            else
                Search_NC();
        }

//SEARCH CƠ BẢN-----------------------------------------------------
        private void Search_CB()
        {
            DataTable _tbTam = Search_CB_Pin();
            ThemVaoLuoi(_tbTam, "Pin");
            _tbTam = Search_CB_ThietBiDien();
            ThemVaoLuoi(_tbTam, "Thiết Bị Điện");
            _tbTam = Search_CB_KichDien();
            ThemVaoLuoi(_tbTam, "Kích Điện");
            _tbTam = Search_CB_AcQuy();
            ThemVaoLuoi(_tbTam, "Ắc Quy");
            _tbTam = Search_CB_MachSac();
            ThemVaoLuoi(_tbTam, "Mạch Sạc");
        }
        //Pin
        private DataTable Search_CB_Pin()
        {
            string sql = "select STT_Pin,MaTB,TenTB,CSKhaDung,TenLoai";
            sql+=" from (select STT_Pin,TenTB,CSKhaDung,MaTB,TenLoai,TienMoiWatt from PIN a,LOAIPIN b where a.MaLoaiPin=b.MaLPin)as T";
            sql += " where T.STT_Pin like '%" + _key + "%' or T.MaTB like '%" + _key + "%' or T.TenTB like '%" + _key + "%'";
            sql += " or T.CSKhaDung like '%" + _key + "%'";
            sql += " or T.MaTB like '%" + _key + "%' or T.TenLoai like '%" + _key + "%' or T.TienMoiWatt like '%" + _key + "%'";
            return Provider.TimKiem(sql);
        }
        //THIETBIDIEN
        private DataTable Search_CB_ThietBiDien()
        {
            string sql = "select STT,MaTB,TenTB,CSKhaDung,TenLoai,CSHaoHut,HSDeBa";
            sql += " from (select STT,TenTB,CSKhaDung,CSHaoHut,MaTB,HSDeBa,TenLoai,GhiChu from THIETBIDIEN a,LOAITBDIEN b where a.MaLoai=b.MaLoai)as T";
            sql += " where T.STT like '%" + _key + "%' or T.MaTB like '%" + _key + "%'";
            sql += " or T.CSKhaDung like '%" + _key + "%' or T.CSHaoHut like '%" + _key + "%' or T.HSDeBa like '%" + _key + "%'";
            sql += " or T.TenTB like '%" + _key + "%' or T.TenLoai like '%" + _key + "%' or T.GhiChu like '%" + _key + "%'";
            return Provider.TimKiem(sql);
        }
        //KICHDIEN
        private DataTable Search_CB_KichDien()
        {
            string sql = "select STT,MaTB,TenTB,CSKhaDung,TenLoai";
            sql += " from (select STT,TenTB,CSKhaDung,MaTB,TenLoai,GhiChu from KICHDIEN a,LOAIKICHDIEN b where a.MaLoai=b.MaLoai)as T";
            sql += " where T.STT like '%" + _key + "%' or T.MaTB like '%" + _key + "%'";
            sql += " or T.CSKhaDung like '%" + _key + "%'";
            sql += " or T.TenTB like '%" + _key + "%' or T.TenLoai like '%" + _key + "%' or T.GhiChu like '%" + _key + "%'";
            return Provider.TimKiem(sql);
        }
        //AcQuy
        private DataTable Search_CB_AcQuy()
        {
            string sql = "select STT,MaTB,TenTB,CSKhaDung,TenLoai";
            sql += " from (select STT,TenTB,CSKhaDung,MaTB,TenLoai,GhiChu from AcQuy a,LOAIACQUY b where a.MaLoai=b.MaLoai)as T";
            sql += " where T.STT like '%" + _key + "%' or T.MaTB like '%" + _key + "%'";
            sql += " or T.CSKhaDung like '%" + _key + "%'";
            sql += " or T.TenTB like '%" + _key + "%' or T.TenLoai like '%" + _key + "%' or T.GhiChu like '%" + _key + "%'";
            return Provider.TimKiem(sql);
        }
        //MACHSAC
        private DataTable Search_CB_MachSac()
        {
            string sql = "select STT,MaTB,TenTB,CSKhaDung";
            sql += " from MACHSAC T";
            sql += " where T.STT like '%" + _key + "%' or T.MaTB like '%" + _key + "%'";
            sql += " or T.CSKhaDung like '%" + _key + "%'";
            sql += " or T.TenTB like '%" + _key + "%' or T.GhiChu like '%" + _key + "%'";
            return Provider.TimKiem(sql);
        }


//SEARCH NÂNG CAO-----------------------------------------------------
        //Pin
        private void Search_NC()
        {
            DataTable _tbTam = new DataTable();
            if (cbbLoaiTB.SelectedIndex == 0)
            {
                _tbTam = Search_NC_Pin();
                ThemVaoLuoi(_tbTam, "Pin");
                return;
            } 
            if (cbbLoaiTB.SelectedIndex == 1)
            {
                _tbTam = Search_NC_ThietBiDien();
                ThemVaoLuoi(_tbTam, "Thiết Bị Điện");
                return;
            }
            if (cbbLoaiTB.SelectedIndex == 2)
            {
                _tbTam = Search_NC_KichDien();
                ThemVaoLuoi(_tbTam, "Kích Điện");
                return;
            }
            if (cbbLoaiTB.SelectedIndex == 3)
            {
                _tbTam = Search_NC_AcQuy();
                ThemVaoLuoi(_tbTam, "Ắc Quy");
                return;
            }
            _tbTam = Search_NC_MachSac();
            ThemVaoLuoi(_tbTam, "Mạch Sạc");
        }

        private DataTable Search_NC_Pin()
        {
            string sql = "select * from (select STT_Pin,MaTB,TenTB,CSKhaDung,TenLoai";
            sql += " from (select STT_Pin,TenTB,CSKhaDung,MaTB,TenLoai,TienMoiWatt from PIN a,LOAIPIN b where a.MaLoaiPin=b.MaLPin)as T";
            sql += " where T.STT_Pin like '%" + _key + "%' or T.MaTB like '%" + _key + "%' or T.TenTB like '%" + _key + "%'";
            sql += " or T.CSKhaDung like '%" + _key + "%'";
            sql += " or T.MaTB like '%" + _key + "%' or T.TenLoai like '%" + _key + "%' or T.TienMoiWatt like '%" + _key + "%') as Y  WHERE 1=1";
            if (!String.IsNullOrEmpty(txtMaTB.Text))
                sql += "AND Y.MaTB ='" + txtMaTB.Text + "'";
            if (!String.IsNullOrEmpty(txtTenTB.Text))
                sql += "AND Y.TenTB LIKE '%" + txtTenTB.Text + "%'";
            if (!String.IsNullOrEmpty(txtCSMin.Text))
                sql += "AND Y.CSKhaDung >= " + txtCSMin.Text;
            if (!String.IsNullOrEmpty(txtCSMax.Text))
                sql += "AND Y.CSKhaDung <= " + txtCSMax.Text;
            return Provider.TimKiem(sql);
        }
        //THIETBIDIEN
        private DataTable Search_NC_ThietBiDien()
        {
            string sql = "select * from (select STT,MaTB,TenTB,CSKhaDung,TenLoai,CSHaoHut,HSDeBa";
            sql += " from (select STT,TenTB,CSKhaDung,CSHaoHut,MaTB,HSDeBa,TenLoai,GhiChu from THIETBIDIEN a,LOAITBDIEN b where a.MaLoai=b.MaLoai)as T";
            sql += " where T.STT like '%" + _key + "%' or T.MaTB like '%" + _key + "%'";
            sql += " or T.CSKhaDung like '%" + _key + "%' or T.CSHaoHut like '%" + _key + "%' or T.HSDeBa like '%" + _key + "%'";
            sql += " or T.TenTB like '%" + _key + "%' or T.TenLoai like '%" + _key + "%' or T.GhiChu like '%" + _key + "%') as Y  WHERE 1=1";
            if (!String.IsNullOrEmpty(txtMaTB.Text))
                sql += "AND Y.MaTB ='" + txtMaTB.Text + "'";
            if (!String.IsNullOrEmpty(txtTenTB.Text))
                sql += "AND Y.TenTB LIKE '%" + txtTenTB.Text + "%'";
            if (!String.IsNullOrEmpty(txtCSMin.Text))
                sql += "AND Y.CSKhaDung >= " + txtCSMin.Text;
            if (!String.IsNullOrEmpty(txtCSMax.Text))
                sql += "AND Y.CSKhaDung <= " + txtCSMax.Text;
            return Provider.TimKiem(sql);
        }
        //KICHDIEN
        private DataTable Search_NC_KichDien()
        {
            string sql = "select * from (select STT,MaTB,TenTB,CSKhaDung,TenLoai";
            sql += " from (select STT,TenTB,CSKhaDung,MaTB,TenLoai,GhiChu from KICHDIEN a,LOAIKICHDIEN b where a.MaLoai=b.MaLoai)as T";
            sql += " where T.STT like '%" + _key + "%' or T.MaTB like '%" + _key + "%'";
            sql += " or T.CSKhaDung like '%" + _key + "%'";
            sql += " or T.TenTB like '%" + _key + "%' or T.TenLoai like '%" + _key + "%' or T.GhiChu like '%" + _key + "%') as Y  WHERE 1=1"; 
            if (!String.IsNullOrEmpty(txtMaTB.Text))
                sql += "AND T.MaTB ='" + txtMaTB.Text + "'";
            if (!String.IsNullOrEmpty(txtTenTB.Text))
                sql += "AND T.TenTB LIKE '%" + txtTenTB.Text + "%'";
            if (!String.IsNullOrEmpty(txtCSMin.Text))
                sql += "AND T.CSKhaDung >= " + txtCSMin.Text;
            if (!String.IsNullOrEmpty(txtCSMax.Text))
                sql += "AND T.CSKhaDung <= " + txtCSMax.Text;
            return Provider.TimKiem(sql);
        }
        //AcQuy
        private DataTable Search_NC_AcQuy()
        {
            string sql = "select * from (select STT,MaTB,TenTB,CSKhaDung,TenLoai";
            sql += " from (select STT,TenTB,CSKhaDung,MaTB,TenLoai,GhiChu from AcQuy a,LOAIACQUY b where a.MaLoai=b.MaLoai)as T";
            sql += " where T.STT like '%" + _key + "%' or T.MaTB like '%" + _key + "%'";
            sql += " or T.CSKhaDung like '%" + _key + "%'";
            sql += " or T.TenTB like '%" + _key + "%' or T.TenLoai like '%" + _key + "%' or T.GhiChu like '%" + _key + "%') as Y  WHERE 1=1";
            if (!String.IsNullOrEmpty(txtMaTB.Text))
                sql += "AND Y.MaTB ='" + txtMaTB.Text + "'";
            if (!String.IsNullOrEmpty(txtTenTB.Text))
                sql += "AND Y.TenTB LIKE '%" + txtTenTB.Text + "%'";
            if (!String.IsNullOrEmpty(txtCSMin.Text))
                sql += "AND Y.CSKhaDung >= " + txtCSMin.Text;
            if (!String.IsNullOrEmpty(txtCSMax.Text))
                sql += "AND Y.CSKhaDung <= " + txtCSMax.Text;
            return Provider.TimKiem(sql);
        }
        //MACHSAC
        private DataTable Search_NC_MachSac()
        {
            string sql = "select * from (select STT,MaTB,TenTB,CSKhaDung";
            sql += " from MACHSAC T";
            sql += " where T.STT like '%" + _key + "%' or T.MaTB like '%" + _key + "%'";
            sql += " or T.CSKhaDung like '%" + _key + "%'";
            sql += " or T.TenTB like '%" + _key + "%' or T.GhiChu like '%" + _key + "%') as Y  WHERE 1=1";
            if (!String.IsNullOrEmpty(txtMaTB.Text))
                sql += "AND Y.MaTB ='" + txtMaTB.Text + "'";
            if (!String.IsNullOrEmpty(txtTenTB.Text))
                sql += "AND Y.TenTB LIKE '%" + txtTenTB.Text + "%'";
            if (!String.IsNullOrEmpty(txtCSMin.Text))
                sql += "AND Y.CSKhaDung >= " + txtCSMin.Text;
            if (!String.IsNullOrEmpty(txtCSMax.Text))
                sql += "AND Y.CSKhaDung <= " + txtCSMax.Text;
            return Provider.TimKiem(sql);
        }

        private string _key;
        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            _key = txtTimKiem.Text;
        }
        private void ThemVaoLuoi(DataTable dulieu,string tennhom)
        {
            for (int i = 0; i < dulieu.Rows.Count; i++)
            {
                DataGridViewRow row = (DataGridViewRow)gvTimkiem.Rows[0].Clone();
                try
                {
                    row.Cells[0].Value = dulieu.Rows[i][0];//STT(Visible)
                    row.Cells[1].Value = dulieu.Rows[i][1];//MaTB
                    row.Cells[2].Value = dulieu.Rows[i][2];////TenTB
                    row.Cells[3].Value = dulieu.Rows[i][3];//CSKhaDung
                    row.Cells[7].Value = tennhom;//Nhom
                    row.Cells[6].Value = dulieu.Rows[i][4];//TenLoai
                }
                catch
                {
                    row.Cells[6].Value = "Không có";//TenLoai
                }
                try
                {
                    row.Cells[4].Value = dulieu.Rows[i][5];//HaoHut
                    row.Cells[5].Value = dulieu.Rows[i][6];//DeBa
                }
                catch
                {
                    row.Cells[4].Value = "Không có";//HaoHut
                    row.Cells[5].Value = "Không có";//DeBa
                }
                gvTimkiem.Rows.Insert(gvTimkiem.Rows.Count - 1, row);
            }
        }
        private void SetupDataGridView(DataGridView gv)
        {
            DataGridViewCellStyle style = gv.ColumnHeadersDefaultCellStyle;
            style.BackColor = Color.Navy;
            style.ForeColor = Color.White;
            style.Font = new Font(gv.Font, FontStyle.Bold);


            gv.GridColor = Color.Blue;  //' Màu các của lưới
            gv.BackgroundColor = Color.AliceBlue; //' Màu nền gridview
            gv.ForeColor = Color.Green; //' Màu chữ
            //' Thiết lập màu chữ cho những dòng có chỉ số lẻ
            gv.AlternatingRowsDefaultCellStyle.ForeColor = Color.Purple;
        }

        private void usTimKiem_Load(object sender, EventArgs e)
        {
            SetupDataGridView(gvTimkiem);
        }
    }
}
